package com.hmxx.myblogproject.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.hmxx.myblogproject.entity.MyBlog;
import com.hmxx.myblogproject.entity.MyBlogTitles;
import com.hmxx.myblogproject.entity.MyBlogUser;
import com.hmxx.myblogproject.mapper.MyBlogBlogMapper;
import com.hmxx.myblogproject.mapper.MyBlogFollowMapper;
import com.hmxx.myblogproject.mapper.MyBlogTitlesMapper;
import com.hmxx.myblogproject.mapper.MyBlogUserMapper;
import com.hmxx.myblogproject.service.MyBlogTitlesService;

import com.hmxx.myblogproject.service.MyBlogUserService;
import com.hmxx.myblogproject.util.JwtHelper;
import com.hmxx.myblogproject.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.logging.Level;

/**
 * @description:
 * @Author: {27106}
 * @Date: {2021/11/18 10:17}
 */
@Service
public class MyBlogTitlesServiceImpl implements MyBlogTitlesService {
    @Resource
    private MyBlogTitlesMapper myBlogTitlesMapper;
    @Resource
    private MyBlogUserMapper myBlogUserMapper;
    @Resource
    private MyBlogBlogMapper myBlogBlogMapper;
    @Resource
    private MyBlogFollowMapper myBlogFollowMapper;

    @Override
    public Object getTitlesList(Integer page, Integer limit,String tagName) {
        PageHelper.startPage(page,limit);
        List<MyBlogTitles> myBlogTitles=myBlogTitlesMapper.getTitlesList();

        if(tagName!=null && !"".equals(tagName)){
            List<MyBlogTitles> myBlogTitlesList=new ArrayList<>();
            for(MyBlogTitles myBlogTitles1 : myBlogTitles){
                String label=myBlogTitles1.getBlogTitleLabel();
                label=label.substring(label.indexOf("[")+1, label.lastIndexOf("]"));

                if(label.contains(tagName)){
                    myBlogTitles1.setBlogTitleLabel(label.replace(","," "));
                    myBlogTitlesList.add(myBlogTitles1);
                }
            }
            return ResponseUtil.okList(myBlogTitlesList);
        }else {
            for (MyBlogTitles myBlogTitles1 : myBlogTitles) {
                String label = myBlogTitles1.getBlogTitleLabel();
                label=label.substring(label.indexOf("[")+1, label.lastIndexOf("]"));
                label = label.replace(",", " ");
                myBlogTitles1.setBlogTitleLabel(label);
            }

            return ResponseUtil.okList(myBlogTitles);
        }
    }

    @Override
    public Object postTitle(String json, HttpServletRequest request) {
        if(null!=JwtHelper.getUserId(request.getHeader("MyBlog-Token"))){
            JSONObject jsonObject= JSON.parseObject(json);
            MyBlogTitles myBlogTitles=new MyBlogTitles();
            myBlogTitles.setBlogTitleHeader(jsonObject.getString("blogTitleHeader"));
            myBlogTitles.setBlogTitleContent(jsonObject.getString("blogTitleContent"));

            String labelStr=jsonObject.getString("blogTitleLabel");
            labelStr=labelStr.substring(labelStr.indexOf("[")+1, labelStr.lastIndexOf("]"));
            String[] labels=labelStr.split(",");
            for(int i=0;i<labels.length;i++){
                labels[i]=labels[i].substring(labels[i].indexOf("\"")+1,labels[i].lastIndexOf("\""));
            }
            myBlogTitles.setBlogTitleLabel(Arrays.toString(labels));

            Integer id= JwtHelper.getUserId(request.getHeader("MyBlog-Token"));
            List<String> uid=new ArrayList<>();
            uid.add(String.valueOf(id));
            List<MyBlogUser> myBlogUser=myBlogUserMapper.queryUserByIds(uid);
            if(!(myBlogUser.size() >0)){
                return ResponseUtil.fail(1,"用户不存在");
            }

            myBlogTitles.setBlogTitleAuthor(myBlogUser.get(0).getUsername());
            int i=myBlogTitlesMapper.insertTitles(myBlogTitles);
            if(i>0){
                Map map=new HashMap<>();
                map.put("titleId", myBlogTitles.getId());
                return ResponseUtil.ok(map);
            }else {
                return ResponseUtil.fail(3,"添加博客失败");
            }
        }else {
            return ResponseUtil.fail(2,"用户登录过期，请重新登录");
        }

    }

    @Override
    public Object getTitleInfo(String id) {

        MyBlogTitles myBlogTitles=myBlogTitlesMapper.queryTitleInfo(id);

        MyBlog myBlog=myBlogBlogMapper.queryBlog(myBlogTitles.getBlogBlogId());

        MyBlogUser myBlogUser=myBlogUserMapper.queryUserByNameAndPwd(myBlogTitles.getBlogTitleAuthor(),null);

        int size=myBlogFollowMapper.queryFollowsByUId(myBlogUser.getId());

        myBlogUser.setFans(String.valueOf(size));
        Map map=new HashMap();
        map.put("title",myBlogTitles.getBlogTitleHeader());
        map.put("introduce",myBlogTitles.getBlogTitleContent());
        map.put("content",myBlog.getBlogBody());
        map.put("authorInfo",myBlogUser);

        return ResponseUtil.ok(map);
    }

    @Override
    public Object getTitlesByUid(String username) {
        if(null!=username){
            List<MyBlogTitles> myBlogTitlesList=myBlogTitlesMapper.queryTitleByUserName(username);
            MyBlogUser myBlogUser=myBlogUserMapper.queryUserByNameAndPwd(username,null);
            int size=myBlogFollowMapper.queryFollowsByUId(myBlogUser.getId());
            myBlogUser.setFans(String.valueOf(size));
            Map map=new HashMap();
            map.put("titleList",myBlogTitlesList);
            map.put("user",myBlogUser);
            return ResponseUtil.ok(map);
        }else {
            return ResponseUtil.fail(1,"参数错误");
        }
    }

    @Override
    public Object getTitleAndBlog(String id) {
        if(null!=id) {
            MyBlogTitles myBlogTitles = myBlogTitlesMapper.queryTitleByBlogId(id);
            MyBlog myBlog = myBlogBlogMapper.queryBlog(id);
            Map map=new HashMap();
            map.put("titles",myBlogTitles);
            map.put("blog",myBlog);
            return ResponseUtil.ok(map);
        }else {
            return ResponseUtil.fail(1,"参数错误");
        }
    }

    @Override
    public Object modifyTitle(String json, HttpServletRequest request) {
        if(null!=JwtHelper.getUserId(request.getHeader("MyBlog-Token"))){
            JSONObject jsonObject= JSON.parseObject(json);
            MyBlogTitles myBlogTitles=new MyBlogTitles();
            //设置标题
            myBlogTitles.setBlogTitleHeader(jsonObject.getString("blogTitleHeader"));
            //设置标题介绍
            myBlogTitles.setBlogTitleContent(jsonObject.getString("blogTitleContent"));
            //设置标签
            String labelStr=jsonObject.getString("blogTitleLabel");
            labelStr=labelStr.substring(labelStr.indexOf("[")+1, labelStr.lastIndexOf("]"));
            String[] labels=labelStr.split(",");
            for(int i=0;i<labels.length;i++){
                labels[i]=labels[i].substring(labels[i].indexOf("\"")+1,labels[i].lastIndexOf("\""));
            }
            myBlogTitles.setBlogTitleLabel(Arrays.toString(labels));
            //设置titleId
//            String tId=myBlogTitlesMapper.queryTitleByBlogId(jsonObject.getString("blogId")).getId();
            myBlogTitles.setBlogBlogId(jsonObject.getString("blogId"));
            int i=myBlogTitlesMapper.updateTitle(myBlogTitles);
            MyBlogTitles mt=myBlogTitlesMapper.queryTitleByBlogId(jsonObject.getString("blogId"));
            if(i>0){
                Map map=new HashMap<>();
                map.put("titleId", mt.getId());
                return ResponseUtil.ok(map);
            }else {
                return ResponseUtil.fail(3,"修改博客失败");
            }

        }else {
            return ResponseUtil.fail(2,"用户登录过期，请重新登录");
        }

    }

    @Override
    public Object delTitleById(String json) {
        JSONObject jsonObject=JSON.parseObject(json);
        String tid=jsonObject.getString("tId");
        String bId=jsonObject.getString("blogId");
        int res=myBlogTitlesMapper.delTitleById(tid);
        if(res>0){
            int r=myBlogBlogMapper.delBlogById(bId);
            if(r>0){
                return ResponseUtil.ok();
            }else {
                return ResponseUtil.fail(2,"删除博客失败");
            }
        }else {
            return ResponseUtil.fail(1,"删除文章失败");
        }

    }

    @Override
    public Object searchTitleList(String json) {
        JSONObject jsonObject=JSON.parseObject(json);
        String select=jsonObject.getString("select");
        String input=jsonObject.getString("input");
        int page= Integer.parseInt(jsonObject.getString("page"));
        int limit= Integer.parseInt(jsonObject.getString("limit"));
        PageHelper.startPage(page,limit);

        List<MyBlogTitles> titlesList=myBlogTitlesMapper.queryTitles(select,input);
        return ResponseUtil.okList(titlesList);
    }


}
